﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Planetwork
{
    public partial class AddMessage : System.Web.UI.Page
    {
        static string conStr = ConfigurationManager.AppSettings["conStr"];
        SqlConnection mycon = new SqlConnection(conStr);

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["index"] == null || Context.Session["name"] == null)
            {
                string url = Request.RawUrl;
                Response.Redirect("../Login/Login.aspx?ReturnUrl=" + url);
            }
           
            this.name.Text = Session["name"].ToString();
           
        }

        /// <summary>
        /// 添加留言
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void submit_Click(object sender, EventArgs e)
        {
            string sql = "insert into Reviews (ParentID, Content, Author) values(@parentID, @content, @author)";
            SqlCommand mycmd = new SqlCommand(sql, mycon);
            mycmd.Parameters.AddWithValue("@parentID", Convert.ToInt32(Request.QueryString["index"]));
            mycmd.Parameters.AddWithValue("@content", this.message.Text);
            mycmd.Parameters.AddWithValue("@author", Session["name"].ToString());

            mycon.Open();
            try
            {
                mycmd.ExecuteNonQuery();
                mycon.Close();
                this.message.Text = "";
                Response.Write("<script>alert('留言成功');</script>");
                
            }
            catch(Exception ex)
            {
                mycon.Close();
                Response.Write("<script>alert('未知错误...');</script>");
            }
            
        }
    }
}